package com.example.warehouse.sys.service.impl;

import com.example.warehouse.common.ex.ServiceException;
import com.example.warehouse.common.pojo.vo.PageData;
import com.example.warehouse.common.security.CurrentPrincipal;
import com.example.warehouse.common.web.ServiceCode;
import com.example.warehouse.sys.dao.persist.respository.INoticeRepository;
import com.example.warehouse.sys.pojo.entity.SysNotice;
import com.example.warehouse.sys.pojo.param.AddNewNoticeParam;
import com.example.warehouse.sys.pojo.vo.*;
import com.example.warehouse.sys.service.INoticeService;
import com.mysql.cj.protocol.x.Notice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author 欧胤
 * @version 1.0
 * @description: 通知业务层
 * @date 2023/7/6 17:55
 */
@Slf4j
@Service
public class NoticeServiceImpl implements INoticeService {

    @Value("${warehouse.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private INoticeRepository repository;

    @Override
    public void addNew(CurrentPrincipal currentPrincipal, AddNewNoticeParam addNewNoticeParam) {
        SysNotice sysNotice = new SysNotice();
        BeanUtils.copyProperties(addNewNoticeParam,sysNotice);

        sysNotice.setUserId(currentPrincipal.getId());
        sysNotice.setName(currentPrincipal.getUsername());
        sysNotice.setEnable(1);
        sysNotice.setSort(99);

        repository.insert(sysNotice);
    }

    @Override
    public void delete(Long id) {
        NoticeStandardVO queryResult = repository.getStandardById(id);
        if (queryResult == null) {
            // 是：数据不存在，抛出异常
            String message = "删除通知失败，尝试删除的通知数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows=repository.deleteById(id);
        if (rows != 1) {
            String message = "删除通知失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public NoticeStandardVO getStandardById(Long id) {
        NoticeStandardVO queryResult = repository.getStandardById(id);
        if (queryResult == null) {
            String message = "查询通知详情失败，通知数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public NoticeStandardVO getStandardByNotice(NoticeSelectVO notice) {
        SysNotice sysNotice = new SysNotice();
        BeanUtils.copyProperties(notice,sysNotice);
        NoticeStandardVO queryResult = repository.getStandardByNotice(notice);
        if (queryResult == null) {
            String message = "查询通知详情失败，通知数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public PageData<NoticeListItemVO> list(Integer pageNum) {
        PageData<NoticeListItemVO> pageData = repository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<NoticeListItemVO> list(Integer pageNum, Integer pageSize) {
        PageData<NoticeListItemVO> pageData = repository.list(pageNum,pageSize);
        return pageData;
    }

    @Override
    public PageData<NoticeListItemVO> list(NoticeSelectVO notice, Integer pageNum) {
        PageData<NoticeListItemVO> pageData =repository.listByNotice(notice,pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<NoticeListItemVO> list(NoticeSelectVO notice, Integer pageNum, Integer pageSize) {
        PageData<NoticeListItemVO> pageData =repository.listByNotice(notice,pageNum, pageSize);
        return pageData;
    }
}
